1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import sun.misc.DoubleConsts;
32 import sun.misc.FpUtils;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class Expm1Tests {
49
50 private Expm1Tests(){}
51
52 static final double infinityD = Double.POSITIVE_INFINITY;
53 static final double NaNd = Double.NaN;
54
55 static int testExpm1() {
56 int failures = 0;
57
58 double [][] testCases = {
59 {Double.NaN, NaNd},
60 {Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
61 {Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
62 {Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
63 {Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
64 {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
65 {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
66 {Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
67 {Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
68 {Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
69 {Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
70 {infinityD, infinityD},
71 {-infinityD, -1.0},
72 {-0.0, -0.0},
73 {+0.0, +0.0},
74 };
75
76
77 for(int i = 0; i < testCases.length; i++) {
78 failures += testExpm1CaseWithUlpDiff(testCases[i][0],
79 testCases[i][1], 0, null);
80 }
81
82
83
84 for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
85 double d = FpUtils.scalb(2, i);
86 failures += testExpm1Case(d, d);
87 failures += testExpm1Case(-d, -d);
88 }
89
90
91
92
93
94
95
96
97
98 for(double d = 37.5; d <= 709.5; d += 1.0) {
99 failures += testExpm1CaseWithUlpDiff(d, StrictMath.exp(d), 2, null);
100 }
101
102
103 for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
104 double d = FpUtils.scalb(2, i);
105 failures += testExpm1Case(d, infinityD);
106 }
107
108
109
110
111 boolean reachedLimit [] = {false, false};
112
113
114
115 for(double d = -36.75; d >= -127.75; d -= 1.0) {
116 failures += testExpm1CaseWithUlpDiff(d, -1.0, 1,
117 reachedLimit);
118 }
119
120 for(int i = 7; i <= DoubleConsts.MAX_EXPONENT; i++) {
121 double d = -FpUtils.scalb(2, i);
122 failures += testExpm1CaseWithUlpDiff(d, -1.0, 1, reachedLimit);
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137 {
138 double pcNeighbors[] = new double[5];
139 double pcNeighborsExpm1[] = new double[5];
140 double pcNeighborsStrictExpm1[] = new double[5];
141
142 for(int i = -50; i <= 50; i++) {
143 double pc = StrictMath.log(2)*i;
144
145 pcNeighbors[2] = pc;
146 pcNeighbors[1] = FpUtils.nextDown(pc);
147 pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
148 pcNeighbors[3] = FpUtils.nextUp(pc);
149 pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
150
151 for(int j = 0; j < pcNeighbors.length; j++) {
152 pcNeighborsExpm1[j] = Math.expm1(pcNeighbors[j]);
153 pcNeighborsStrictExpm1[j] = StrictMath.expm1(pcNeighbors[j]);
154 }
155
156 for(int j = 0; j < pcNeighborsExpm1.length-1; j++) {
157 if(pcNeighborsExpm1[j] > pcNeighborsExpm1[j+1] ) {
158 failures++;
159 System.err.println("Monotonicity failure for Math.expm1 on " +
160 pcNeighbors[j] + " and " +
161 pcNeighbors[j+1] + "\n\treturned " +
162 pcNeighborsExpm1[j] + " and " +
163 pcNeighborsExpm1[j+1] );
164 }
165
166 if(pcNeighborsStrictExpm1[j] > pcNeighborsStrictExpm1[j+1] ) {
167 failures++;
168 System.err.println("Monotonicity failure for StrictMath.expm1 on " +
169 pcNeighbors[j] + " and " +
170 pcNeighbors[j+1] + "\n\treturned " +
171 pcNeighborsStrictExpm1[j] + " and " +
172 pcNeighborsStrictExpm1[j+1] );
173 }
174
175
176 }
177
178 }
179 }
180
181 return failures;
182 }
183
184 public static int testExpm1Case(double input,
185 double expected) {
186 return testExpm1CaseWithUlpDiff(input, expected, 1, null);
187 }
188
189 public static int testExpm1CaseWithUlpDiff(double input,
190 double expected,
191 double ulps,
192 boolean [] reachedLimit) {
193 int failures = 0;
194 double mathUlps = ulps, strictUlps = ulps;
195 double mathOutput;
196 double strictOutput;
197
198 if (reachedLimit != null) {
199 if (reachedLimit[0])
200 mathUlps = 0;
201
202 if (reachedLimit[1])
203 strictUlps = 0;
204 }
205
206 failures += Tests.testUlpDiffWithLowerBound("Math.expm1(double)",
207 input, mathOutput=Math.expm1(input),
208 expected, mathUlps, -1.0);
209 failures += Tests.testUlpDiffWithLowerBound("StrictMath.expm1(double)",
210 input, strictOutput=StrictMath.expm1(input),
211 expected, strictUlps, -1.0);
212 if (reachedLimit != null) {
213 reachedLimit[0] |= (mathOutput == -1.0);
214 reachedLimit[1] |= (strictOutput == -1.0);
215 }
216
217 return failures;
218 }
219
220 public static void main(String argv[]) {
221 int failures = 0;
222
223 failures += testExpm1();
224
225 if (failures > 0) {
226 System.err.println("Testing expm1 incurred "
227 + failures + " failures.");
228 throw new RuntimeException();
229 }
230 }
231 }